package com.pureut.system.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.system.domain.MaterialBomHalf;
import com.pureut.system.domain.dto.MaterialBomHalfDto;
import com.pureut.system.domain.vo.MaterialBomVo;
import com.pureut.system.domain.vo.MaterialNewBomAddVo;
import com.pureut.system.service.IMaterialBomHalfService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新Bom
 * @author: CM
 * @date: 2023/6/13  10:07
 */
@RestController
@RequestMapping("/newBom")
public class MaterialBomHalfController extends BaseController {

    @Resource
    private IMaterialBomHalfService materialBomHalfService;

    /**
     * 获取BOM配置
     */
    @RequiresPermissions("material:bom:list")
    @GetMapping("/list")
    public AjaxResult list(MaterialBomVo materialBomVo) {

        List<MaterialBomHalfDto> list = materialBomHalfService.selectBomList(materialBomVo);
        return AjaxResult.success(list);
    }

    /**
     * 新增Bom信息
     */
    @RequiresPermissions("material:bom:add")
    @Log(title = "新增Bom信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MaterialNewBomAddVo materialNewBomAddVo) throws Exception {
        return toAjax(materialBomHalfService.insertBom(materialNewBomAddVo));
    }

    /**
     * 查看Bom信息
     */
    @GetMapping(value = "/view/{bomId}")
    @RequiresPermissions("material:bom:query")
    public AjaxResult view(@PathVariable long bomId) throws Exception {
        return AjaxResult.success(materialBomHalfService.view(bomId));
    }


    /**
     * 删除bom信息
     */
    @Log(title = "删除bom信息", businessType = BusinessType.DELETE)
    @RequiresPermissions("material:bom:remove")
    @DeleteMapping("/delete/{bomId}")
    public AjaxResult remove(@PathVariable("bomId") long bomId) throws Exception {
        return toAjax(materialBomHalfService.deleteMaterialBomById(bomId));
    }

    /**
     * 修改bom信息
     */
    @Log(title = "修改bom信息", businessType = BusinessType.UPDATE)
    @RequiresPermissions("material:bom:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MaterialNewBomAddVo materialNewBomAddVo) throws Exception {
        return toAjax(materialBomHalfService.updateMaterialBomEdit(materialNewBomAddVo));
    }

    /**
     * 获取bom第一层级数据列表（委外订单新增使用）
     * @param materialBomVo
     * @return
     */
    @GetMapping("/getOneLevelList")
    public TableDataInfo getOneLevelList(MaterialBomVo materialBomVo) {
        startPage();
        List<MaterialBomHalfDto> list = materialBomHalfService.getOneLevelList(materialBomVo);
        return getDataTable(list);
    }

    /**
     * 获取BOM信息(库存量)
     */
    @GetMapping("/detailInventory")
    public AjaxResult listDetailInventory(long bomId) {
        return AjaxResult.success(materialBomHalfService.selectlBomDetailListInventory(bomId));
    }

    /**
     * 获取BOM信息(脱脂查询使用库存量)
     */
    @GetMapping("/detailInventoryTz")
    public AjaxResult listDetailInventoryTz(long bomId) {
        return AjaxResult.success(materialBomHalfService.selectlBomDetailListInventoryTz(bomId));
    }

    /**
     * 复制BOM列表
     */
    @GetMapping("/copyBom")
    public AjaxResult copyBomList(MaterialBomHalf materialBomHalf) {
        return AjaxResult.success(materialBomHalfService.copyBomList(materialBomHalf));
    }

    /**
     * Bom树结构列表
     */
    @GetMapping("/BomList")
    public TableDataInfo BomList(MaterialBomHalf materialBomHalf) {
        startPage();
        List<MaterialBomHalfDto> list = materialBomHalfService.BomList(materialBomHalf);
        return getDataTable(list);
    }

    /**
     * 根据编码获取半成品bom信息(fegin调用)
     */
    @GetMapping("/getHalfBomDetailData")
    public AjaxResult getHalfBomDetailData(String materialCode) {
        return AjaxResult.success(materialBomHalfService.getHalfData(materialCode));
    }



}
